package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.ProfileKeysModel;
import org.testis.gui.mvc.utilities.AtofProfileKeyTableModel;
import org.testis.gui.mvc.view.testdesign.ProfileKeysView;
import java.util.HashMap;
import java.util.List;

import org.testis.utils.middleware.GlobalDataHandler;

/**
 *
 * @author nurhat
 */
public class ProfileKeysController extends AbstractController {

    public static final String SHOW_VIEW = "ViewVisibility";
    public static final String PARAMETER_MODEL_CHANGED = "Model";
    public static final String SHOW_ERROR = "LastError";

    public ProfileKeysController() {
        super();
        TestDesignControllerList.newInstance().setProfileKeysController(this);

    }

    public void startLoader() {
        initialize();
        setModelProperty(SHOW_VIEW,
                true);

    }

    public void operationCloseWindow() {
        this.cleanAll();
    }

    public void initialize() {
        AtofProfileKeyTableModel temp = new AtofProfileKeyTableModel();
        temp.addColumn("Id");
        temp.addColumn("Name");
        temp.addColumn("Groupname");
        temp.addColumn("Priority");
        ProfileKeysModel model = new ProfileKeysModel(temp);
        ProfileKeysView view = new ProfileKeysView(TestDesignControllerList.newInstance().
                getParentFrame(),
                true,
                temp);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public HashMap operationGetKeys() {
        GlobalDataHandler handler = new GlobalDataHandler();
        return handler.getProfileKeysAsMap();
    }

    public List<String[]> operationGetKeysAsArray() {
        GlobalDataHandler handler = new GlobalDataHandler();
        return handler.getProfileKeysAsArray();
    }
     public List<String> operationGetProfileKeysGroupNames() {
        GlobalDataHandler handler = new GlobalDataHandler();
        return handler.getProfileKeysGroupNames();
    }
    public void operationLoadKeys() { 
        List<String[]> hmap =operationGetKeysAsArray();
        AtofProfileKeyTableModel temp = new AtofProfileKeyTableModel();
        temp.addColumn("Id");
        temp.addColumn("Name");
        temp.addColumn("Groupname");
        temp.addColumn("Priority");
        for (int i = 0; i < hmap.size(); i++) {
            temp.addRow(hmap.get(i));

        }
        setModelProperty(PARAMETER_MODEL_CHANGED,
                temp);
    }

    public void operationAddKey(String name, String groupName, String priority) {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.saveProfileKey(0,
                name, groupName, priority);
        if (result) {
            operationLoadKeys();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to add record...");
        }
    }

    public void operationEditKey(int id,
            String name, String groupName, String priority) {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.saveProfileKey(id,
                name, groupName, priority);
        if (result) {
            operationLoadKeys();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to edit record...");
        }
    }

    public void operationRemoveKey(int id) {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.removeProfileKey(id);
        if (result) {
            operationLoadKeys();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to remove record...");
        }
    }

    public void operationRemoveKeyByName(String name) {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.removeProfileKey(name);
        if (result) {
            operationLoadKeys();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to remove record...");
        }
    }
}
